//
//  AppConf.m
//  WeddingStoreFinder
//
//  Created by Macdev on 11/19/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "AppConf.h"
#import "AppDelegate.h"



@interface AppConf()


+ (NSArray*) loadAllAppconf;
+ (void) insertAppConfLine:(NSString *)confName_ value:(NSString *)confValue_;
+ (void) initHashMap;
@end

static NSMutableDictionary *hashMap;
static MKUserLocation *location;

@implementation AppConf

@dynamic confName;
@dynamic confValue;

+ (void) initAppConf {
	[AppConf initHashMap];
	
	NSArray *appConfValues = [AppConf loadAllAppconf];
	
	for (AppConf *loadedAppConf in appConfValues) {
		[hashMap setValue:loadedAppConf.confValue forKey:loadedAppConf.confName];
		
	} 
}

+ (NSArray*) loadAllAppconf {
	AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
	NSManagedObjectContext *managedObjectContext = delegate.managedObjectContext;
	
	[managedObjectContext deletedObjects];
	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	NSEntityDescription *entity = [NSEntityDescription entityForName:@"AppConf" inManagedObjectContext:managedObjectContext];
	[request setEntity:entity];
	
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"confName" ascending:YES];
	NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
	[request setSortDescriptors:sortDescriptors];
	[sortDescriptor release];
	[sortDescriptors release];
	
	NSError *error = nil;
	NSArray *result = [managedObjectContext executeFetchRequest:request error:&error];
	[request release];
	if (error) {
		NSLog(@"Error(AppConf-initAppconf): %@", [error description]);
	}
	return result;
}


+ (void) save {
	//save data to db:
	AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
	NSManagedObjectContext *managedObjectContext = delegate.managedObjectContext;
	
	NSArray *appConfValues = [AppConf loadAllAppconf];
	
	for (AppConf *appConf in appConfValues) {
		
        [managedObjectContext deleteObject:appConf];
		
	}
    
    
    
	//save appconf values:
	if ([hashMap count] > 0) {
		NSArray *keys = [hashMap allKeys];
		for (NSString *key in keys) {
			[AppConf insertAppConfLine:key value:[hashMap valueForKey:key]];
		}
	}
    
	NSError *error;
	if (![managedObjectContext save:&error]) {
		NSLog(@"Error(AppConf-saveAppConf): %@", [error description]);
	}
}

+ (void) insertAppConfLine:(NSString *)confName_ value:(NSString *)confValue_ {
	AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
	NSManagedObjectContext *managedObjectContext = delegate.managedObjectContext;
	AppConf *appConf = (AppConf *)[NSEntityDescription insertNewObjectForEntityForName:@"AppConf" inManagedObjectContext:managedObjectContext];
	appConf.confName = confName_;
	appConf.confValue = confValue_;
	NSError *error;
	if (![managedObjectContext save:&error]) {
		NSLog(@"Error(AppConf-insertAppConfLine): %@", [error description]);
	}
}


+ (void) initHashMap {
	hashMap = [[NSMutableDictionary alloc] init];
	[hashMap setValue:@"0" forKey:kAppConfKey_LastUpdateTimeStamp];

}

#pragma mark -
#pragma mark getter and setter methods


//get an int value for a key:
+ (int) getIntForKey:(NSString*)key {
	return [(NSString*)[hashMap valueForKey:key] intValue];
}
//set an int value for a key:
+ (void) setInt:(int)value forKey:(NSString*)key {
	[hashMap setValue:[NSString stringWithFormat:@"%d", value] forKey:key];
}
//get a double value for a key:
+ (double) getDoubleForKey:(NSString*)key {
	return [(NSString*)[hashMap valueForKey:key] doubleValue];
}
//set a double value for a key:
+ (void) setDouble:(double)value forKey:(NSString*)key {
	[hashMap setValue:[NSString stringWithFormat:@"%f", value] forKey:key];
}
//get a string value for a key:
+ (NSString*) getStringForKey:(NSString*)key {
	return (NSString*)[hashMap valueForKey:key];
}
//set a string value for a key:
+ (void) setString:(NSString*)value forKey:(NSString*)key {
	[hashMap setValue:value forKey:key];
}

//get a BOOL value for a key:
+ (BOOL) getBOOLForKey:(NSString*)key {
	return [[hashMap valueForKey:key] isEqualToString:kBoolStringValue_True];
}
//set a BOOL value for a key:
+ (void) setBOOL:(BOOL)value forKey:(NSString*)key {
	if (value) {
		[hashMap setValue:@"1" forKey:key];
	} else {
		[hashMap setValue:@"0" forKey:key];
	}
	
}
//get a long long value for a key:
+ (long long) getLongLongForKey:(NSString*)key {
	return [(NSString*)[hashMap valueForKey:key] longLongValue];
}
//set a long longs value for a key:
+ (void) setLongLong:(long long)value forKey:(NSString*)key {
	[hashMap setValue:[NSString stringWithFormat:@"%qi", value] forKey:key];
}
//get a float value for a key:
+ (float) getFloatForKey:(NSString*)key {
	return [(NSString*)[hashMap valueForKey:key] floatValue];
}
//set a float value for a key:
+ (void) setFloat:(float)value forKey:(NSString*)key {
	[hashMap setValue:[NSString stringWithFormat:@"%f", value] forKey:key];
}

+ (void) setUserLocation:(MKUserLocation*)newValue {
	if (location) {
		[location release];
	}
	location = [newValue retain];
}

+ (MKUserLocation*) getUserLocation {
	return location;
}



+ (void) releaseAll {
	if (hashMap) {
		[hashMap release];
	}
	if (location) {
		[location release];
	}
    
}

@end
